package com.elf.zero.form;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.databinding.BindingAdapter;
import androidx.databinding.BindingMethods;

import com.elf.zero.app.AbsLinearLayout;

public class FormTextView extends AbsLinearLayout {

    protected TextView tv_must, tv_text, txt_value;
    protected ImageView iv_navigate;

    public FormTextView(Context context) {
        super(context);
    }

    public FormTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.zero_form_text_view;
    }

    @Override
    protected void initView(AttributeSet attrs) {
        tv_text = findViewById(R.id.tv_text);
        txt_value = findViewById(R.id.txt_value);
        iv_navigate = findViewById(R.id.iv_navigate);
        tv_must = findViewById(R.id.tv_must);
        tv_must.setVisibility(GONE);

        if (attrs != null) {
            TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.FormTextView);
            tv_text.setText(typedArray.getString(R.styleable.FormTextView_form_text));
            txt_value.setText(typedArray.getString(R.styleable.FormTextView_form_value));
            String hint = typedArray.getString(R.styleable.FormTextView_form_hint);
            if (TextUtils.isEmpty(hint)) {
                txt_value.setHint(R.string.zero_form_hint);
            } else {
                txt_value.setHint(hint);
            }
            boolean showNavigate = typedArray.getBoolean(R.styleable.FormTextView_form_navigate, true);
            showNavigate(showNavigate);
            int gravity = typedArray.getInt(R.styleable.FormTextView_form_gravity, -1);
            if (gravity != -1) {
                setGravity(gravity);
            }
            typedArray.recycle();
        }
    }

    public void setText(String text) {
        tv_text.setText(text);
    }

    public String getText() {
        return tv_text.getText().toString();
    }

    public void setValue(String value) {
        txt_value.setText(value);
    }

    public String getValue() {
        return txt_value.getText().toString();
    }

    public void setMust(boolean b) {
        tv_must.setVisibility(b ? VISIBLE : GONE);
    }

    public void setGravity(int gravity){
        txt_value.setGravity(gravity);
    }

    public void showNavigate(boolean b) {
        if (iv_navigate != null) {
            iv_navigate.setVisibility(b ? VISIBLE : GONE);
        }
    }

    public TextView getValueTextView(){
        return txt_value;
    }
}
